import { EPokerStatus, Esuit } from "../ConfigEnum";
import GameEvent from "./GameEvent";
import GameCtrl from './GameCtrl';
import UIPoker from "../UIPoker";
import SplashScene from "../SplashScene";
import GameScene from "./GameView";
import _Event from "../base/event";


export class Poker{
    public point :number = -1;
    public suit: Esuit = Esuit.HEITAO;//花色
    public _status: EPokerStatus = EPokerStatus.CLOSE;
    public get status(): EPokerStatus {
        return this._status;
    }
    public set status(value: EPokerStatus) {
        this._status = value;
    }
    public get view() :UIPoker{
        return this._view;
    }
    private _view:UIPoker = null;

    
    constructor(point:number,suit:Esuit,pokerstatus:EPokerStatus){
        this.point = point;
        this.suit = suit;
        this._status = pokerstatus;
    }

    public Bind(view:UIPoker){
        this._view = view;
    }
    public UnBind(){
        this._view = null;
    }
}
class PokerGroup{
    public AddPoker(poker:Poker){
        this._pokers.push(poker);
    }
    public get pokers(): Poker[] {
        return this._pokers;
    }
    private _pokers: Poker[] = [];
}

//裁判，隶属于GameScene,牌局管理者，准备好扑克牌，桌子等等
export default class GameDB {

    private m_GameCtrl : GameCtrl = null;
    
    /*********************************************************************************
     * property
     ******************************************************************************/

    private _pokers: Poker[] = [];
    //发牌区
    private _closeAreaPokers: Poker[] = [];
    private _openAreaPokers: Poker[] = [];
    //收牌区
    private _receiveAreaPokerGroups: PokerGroup[] = [];
    //玩牌区
    private _playAreaPokerGroups: PokerGroup[] = [];

    
    /*******************************************************************************
     * Private API
     ******************************************************************************/
    constructor(){
        //初始化牌局结构
        for(let i = 0;i<GameDB.CONST_RECEIVE_GROUPS;i++){
            let pokerGroup = new PokerGroup();
            this._receiveAreaPokerGroups.push(pokerGroup);
        }
        for(let i = 0;i<GameDB.CONST_PLAY_GROUPS;i++){
            let pokerGroup = new PokerGroup();
            this._playAreaPokerGroups.push(pokerGroup);
        }
        //初始化扑克牌
        for(let point =1;point <=13 ;point++){
            for(let suit = 0;suit<4;suit++){
                let poker= new Poker(point,suit,EPokerStatus.CLOSE);
                this._pokers.push(poker);
            }
        }
        //初始化牌局事件
        SplashScene.eventTarget.emit(GameEvent.INIT_POKER,this._pokers)
        
    }

    /*******************************************************************************
     * getter & setter
     ******************************************************************************/
    public get pokers(): Poker[] {
        return this._pokers;
    }
    public get receiveAreaPokerGroups(): PokerGroup[] {
        return this._receiveAreaPokerGroups;
    }
    public get playAreaPokerGroups(): PokerGroup[] {
        return this._playAreaPokerGroups;
    }
    public get closeAreaPokers(): Poker[] {
        return this._closeAreaPokers;
    }
    public get openAreaPokers(): Poker[] {
        return this._openAreaPokers;
    }

    /*******************************************************************************
     * Public static API
     ******************************************************************************/
    public static Create():GameDB{
       let gameDB = new GameDB();
       return gameDB;
    }
    public static readonly CONST_RECEIVE_GROUPS:number = 4;
    public static readonly CONST_PLAY_GROUPS:number = 7;
    /*******************************************************************************
     * Public API
     ******************************************************************************/
    public Play(){
        //洗牌
        this.shuffle(this._pokers,500);
        // 移动牌
        let tmp = this._closeAreaPokers
        this._closeAreaPokers =this._pokers
        this._pokers = tmp
        //通知UI层，发生变化
        SplashScene.eventTarget.emit(GameEvent.PLAY,this._pokers)

        //发牌
        for(let cards = GameDB.CONST_PLAY_GROUPS;cards>=1;cards--){
            for(let i = 0;i<cards;i++){
                let cardGroupIndex = GameDB.CONST_PLAY_GROUPS - cards +i
                let cardGroup: PokerGroup = this._playAreaPokerGroups[cardGroupIndex];
                let poker = this._closeAreaPokers[this._closeAreaPokers.length-1]
                this._closeAreaPokers.length = this._closeAreaPokers.length-1;
                poker.status = i === 0?EPokerStatus.OPEN : EPokerStatus.CLOSE;
                // console.info("发牌",poker.point,poker.suit,poker.status)
                
                cardGroup.AddPoker(poker);
               SplashScene.eventTarget.emit(GameEvent.INIT_GROUP_CARD,cardGroupIndex,GameDB.CONST_PLAY_GROUPS -cards,poker) 
            }
        }        
    }

    /*******************************************************************************
     * Event handle
     ******************************************************************************/
    //洗牌
    private shuffle(pokers : Poker[],count: number =100){
        for (let i=0;i<count;i++){
            let sIdx = parseInt('' + Math.random() * pokers.length, 10);
            let eIdx = parseInt('' + Math.random() * pokers.length, 10);
            let tmp = pokers[sIdx];
            pokers[sIdx] = pokers[eIdx];
            pokers[eIdx] = tmp;
        }
        
    }
    
    public OnEventClickPoker(poker:Poker) {
        console.log('当前牌：',poker)
    }

}